<?php
namespace app\models;

use Yii;
use yii\db\Expression;
use yii\base\Model;

class NavSearch extends Nav
{

    public function rules()
    {
        return [
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                            'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                            'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['parentid', 'integer', 'message' => 'parentid必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'parentid最小存储0', 
                         'max'=>65535, 'tooBig' => 'parentid最大存储65535'],
            ['display', 'default', 'value' => 0],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(0, 1, 2), 'message' => 'display值必须在0,1,2中选择'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['NavSearch'] = $params;
        // P($data);

        $query = Nav::find();

        if (!($this->load($data) && $this->validate())) {
            return false;
        }
        // P($this);

        if(isset($this->parentid)){
            // $query -> andWhere([
            //     'parentid' => $this->parentid,
            // ]);  
            $where = new Expression("FIND_IN_SET(:allparentid, allparentid)", [":allparentid" => $this->parentid]);
            $query -> andWhere($where); 
        }

        if(isset($this->app_id)){
            $query -> andWhere([
                'app_id' => $this->app_id,
            ]);            
        }

        if($this->display == 0){
            $query -> andWhere(['or',
                ['{{%nav}}.display' => 1],
                ['{{%nav}}.display' => 2]
            ]);
        }else{
            $query -> andWhere([
                '{{%nav}}.display' => $this->display,
            ]);            
        }

        // P($query);

        return $query;
    }

    
}